﻿using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace blackhouse.StareKino.Web.Controls {
    public partial class SelectLanguage : System.Web.UI.UserControl {

        #region Properties

        [Browsable(true), DefaultValue(true)]
        public bool TextUpperCase { get; set; }

        #endregion

        #region Constructors

        public SelectLanguage() {
            this.TextUpperCase = true;
        }

        #endregion

        #region Methods

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            this.rptLanguages.ItemDataBound += rptLanguages_ItemDataBound;
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            int sCount = SiteSettings.SiteCount();
            int currentSiteId = CacheHelper.GetCurrentSiteSettings().SiteId;
            if (sCount > 1) {
                SiteLanguage[] langs = new SiteLanguage[sCount];
                for (int i = 1; i <= sCount; i++) {
                    string sCulture = ConfigurationManager.AppSettings[String.Format("site{0}culture", i)];
                    CultureInfo siteCulture = CultureInfo.GetCultureInfo(sCulture, "pl-PL");
                    SiteSettings ss = new SiteSettings(i);
                    SiteFolder sf = SiteFolder.GetBySite(ss.SiteGuid).FirstOrDefault();
                    SiteLanguage sl = new SiteLanguage();
                    sl.IsActive = i == currentSiteId;
                    if (sf != null)
                        sl.UrlToSite = "/" + sf.FolderName;
                    else
                        sl.UrlToSite = "/";
                    sl.Text = siteCulture.Name.Substring(0, 2);
                    sl.Tooltip = siteCulture.NativeName;
                    langs[i - 1] = sl;

                }
                this.rptLanguages.DataSource = langs;
                this.rptLanguages.DataBind();
            } else
                this.rptLanguages.Visible = false;
        }

        protected void rptLanguages_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            switch (e.Item.ItemType) {
                case ListItemType.AlternatingItem:
                case ListItemType.Item:
                    SiteLanguage sl = (SiteLanguage)e.Item.DataItem;
                    if (!String.IsNullOrEmpty(sl.Text)) {
                        StringBuilder sb = new StringBuilder();
                        sb.Append("<li");
                        if (sl.IsActive)
                            sb.Append(" class='active'");
                        sb.Append(">");
                        if (!sl.IsActive)
                            sb.AppendFormat("<a href='{0}' title='{1}'>", sl.UrlToSite, sl.Tooltip);
                        sb.Append((this.TextUpperCase) ? sl.Text.ToUpper() : sl.Text);
                        if (!sl.IsActive)
                            sb.Append("</a>");
                        sb.Append("</li>");
                        e.Item.Controls.Clear();
                        e.Item.Controls.Add(new Literal() { Text = sb.ToString() });
                    } else
                        e.Item.Visible = false;
                    break;
            }
        }

        #endregion

    }
    public struct SiteLanguage {
        public bool IsActive;
        public string UrlToSite;
        public string Text;
        public string Tooltip;
    }
}